package com.epalmpay.util.code;


import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Hashtable;

import javax.imageio.ImageIO;

import com.epalmpay.util.Tools;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.Binarizer;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.ChecksumException;
import com.google.zxing.EncodeHintType;
import com.google.zxing.FormatException;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageConfig;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.QRCodeReader;

public class QRCode {

	private static final String FORMAT="PNG";
	
	/**
	 * 生成二维码
	 * @param contents 内容，换行可以用\n
	 * @param width 宽度
	 * @param height 高度
	 * @throws WriterException
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	public static BufferedImage encode(QRCodeParams params) throws WriterException, IOException{
		String contents=null;
		if(Tools.notEmpty(params.getCardNo())){
			String code=params.getTxt()+"&"+params.getCardNo();
			 contents=new String(code.getBytes(params.getCharset()),"ISO-8859-1");
		}else{
			 contents=new String(params.getTxt().getBytes(params.getCharset()),"ISO-8859-1");
		}
		Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();    
        // 指定纠错等级 二维码容错率    
        hints.put(EncodeHintType.ERROR_CORRECTION, params.getLevel());    
        // 指定编码格式    
        hints.put(EncodeHintType.CHARACTER_SET, params.getCharset());   
        //白边大小，取值范围0~4  
        hints.put(EncodeHintType.MARGIN, params.getMargin()); 
        int onColor = params.getOnColor();     //前景色  
        int offColor = params.getOffColor();    //背景色  
        
        MatrixToImageConfig config = new MatrixToImageConfig(onColor, offColor);  
        BitMatrix bitMatrix = new MultiFormatWriter().encode(contents,BarcodeFormat.QR_CODE, params.getWidth(), params.getHeight(), hints);    
        return MatrixToImageWriter.toBufferedImage(bitMatrix, config);
		
	}
 
 
	/**
	 * 从一张图片解析出二维码信息
	 * @param dest 目标地址
	 * @return String 二维码信息
	 * @throws IOException
	 * @throws NotFoundException
	 * @throws ChecksumException
	 * @throws FormatException
	 */
	public static String decode(String dest) throws IOException, NotFoundException, ChecksumException, FormatException{
		QRCodeReader reader=new QRCodeReader();
		BufferedImage image=ImageIO.read(new File(dest));
		LuminanceSource source=new BufferedImageLuminanceSource(image);
		Binarizer binarizer = new HybridBinarizer(source);
		BinaryBitmap imageBinaryBitmap = new BinaryBitmap(binarizer  );  
		Result result = reader.decode(imageBinaryBitmap); 
        return result.getText();
	}
	
	

}
